class Solution:

    def distributeCandies(self, n: int, limit: int) -> int:

        def min(a, b):
            return a if a <= b else b

        def max(a, b):
            return a if a >= b else b

        tlimit = min(limit << 1, n)
        ans = 0
        for i in range(max(0, n - tlimit), min(n, limit) + 1):
            L = n - i
            ans += min(L, limit) - max(L - limit, 0) + 1

        return ans
